package com.pesking.core.sqlparser;

import java.util.HashMap;
import java.util.Map;

import com.pesking.core.parameter.Parameter;
import com.pesking.core.util.StringUtils;

public class ValueNameSqlResult extends SqlResult<Map<String, String>> {

	public ValueNameSqlResult(Parameter requestParameter) {
		super(requestParameter);
	}
	
	public ValueNameSqlResult(Parameter requestParameter,String valueField,String nameField){
		super(requestParameter);
		this.valueField = valueField;
		this.nameField = nameField;
	}

	@Override
	protected Map<String, String> newResult() {
		return new HashMap<>();
	}

	@Override
	protected void dealColumn(ColumnInfo ci, String columnName, String columnValue) {
		if ((!StringUtils.isBlank(valueField) && valueField.equals(columnName))
				|| this.columns == 0){
			this.value = columnValue;
		}
		else if ((!StringUtils.isBlank(nameField) && nameField.equals(columnName))
				|| this.columns == 1){
			this.name = columnValue;
		}
		this.columns++;
	}

	
	@Override
	protected void afterResult() {
		this.result.put(name, value);
	}
	
	private String value = null;
	private String name  = null;
	private String valueField = null;
	private String nameField  = null;
	private int columns = 0;
}
